import axios from 'axios'
import authUtils from './authUtils'
let gRouter = null;
let gVue = null;

const service = axios.create({
    baseURL: window.rootPath,
    timeout: 10000,
    headers: {
        'Content-Type': 'application/json;charset=utf-8',
        'X-Requested-With': 'XMLHttpRequest',
        'common':{
            "Accept": "application/json;charset=utf-8"
        }        
    }
    // withCredentials: true, // send cookies when cross-domain requests
});

// request interceptor
service.interceptors.request.use(
    config => {
        //console.log(config)
        return config
    },
    error => {
        return Promise.reject(error)
    }
);
  
// response interceptor
service.interceptors.response.use(

    response => {
        //console.log(this)
        const res = response.data
        //console.log(res)
        if( res.error_code == 10401 ){
            gVue.nextTick(function () {
                authUtils.logout(gRouter);
            });            
        }
        return response;
    },
    error => {
        return Promise.reject(error)
    }
);

export function initRequest(vue,router){
    gVue = vue;
    gRouter = router;
    return {service,axios}
}
export default {service,axios}